package com.atguigu.srb.core.controller.admin;


import com.atguigu.srb.base.util.Asserts;
import com.atguigu.srb.common.exception.BusinessException;
import com.atguigu.srb.common.result.R;
import com.atguigu.srb.common.result.ResponseEnum;
import com.atguigu.srb.core.entity.IntegralGrade;
import com.atguigu.srb.core.service.IntegralGradeService;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 积分等级表 前端控制器
 * </p>
 *
 * @author Atguigu
 * @since 2023-06-30
 */
@RestController
@RequestMapping("/admin/core/integralGrade")
@Api(tags = "管理员积分管理模块")
@Transactional
//允许跨域
// @CrossOrigin
public class AdminIntegralGradeController {
    //查询所有
    @Autowired
    private IntegralGradeService IGService;
    @Transactional(readOnly = true) //事务只读,提升查询的效率
    @ApiOperation("查询所有的积分详情")
    @GetMapping
    public R getAll(){
        List<IntegralGrade> list = IGService.list();
        return R.ok().data("list",list);
    }
    //查询所有,带分页功能的
    @Transactional(readOnly = true) //事务只读,提升查询的效率
    @ApiOperation("分页查询积分等级")
    @GetMapping("/{pageNum}/{pageSize}")
    public R getPageList(
            @ApiParam(name = "pageNum",value = "当前的页码",required = true)@PathVariable("pageNum")Integer pageNum,
            @ApiParam(name = "pageSize",value = "当前的页面条数",required = true)@PathVariable("pageSize")Integer pageSize
    ){
        /* try {
            int i = 1 / 0;
            //查询分页的信息
            return R.ok().data("pageInfo",IGService.page(new Page(pageNum,pageSize)));
        } catch (Exception e) {
            throw new BusinessException(ResponseEnum.ARITHMETIC_DIVIDE_ZERO_ERROR,e);
        } */
        return R.ok().data("pageInfo",IGService.page(new Page(pageNum,pageSize)));
    }
    @ApiOperation("根据id查询积分信息")
    @Transactional(readOnly = true) //事务只读,提升查询的效率
    @GetMapping("/{id}")
    public R getById(
            @ApiParam(name = "id",value = "需要查询的积分信息的id",required = true)
            @PathVariable("id")Long id
    ){
        IntegralGrade in = IGService.getById(id);
        System.out.println(in);
        Asserts.AssertNotNull(in,ResponseEnum.DATA_NULL_ERROR);
        return R.ok().data("obj",IGService.getById(id));
    }

    //保存对象
    @ApiOperation("保存积分信息")
    @PostMapping
    public R insert(@ApiParam(name = "obj",value = "新增的对象",required = true) @RequestBody IntegralGrade obj){
        boolean save = IGService.save(obj);
        Asserts.AssertTrue(save,ResponseEnum.SAVE_FAIL);
        // IntegralGrade borrowAmount = IGService.getOne(new QueryWrapper<IntegralGrade>().eq("borrowAmount", obj.getBorrowAmount()));
        IntegralGrade one = IGService.getOne(Wrappers.lambdaQuery(IntegralGrade.class).eq(IntegralGrade::getBorrowAmount, obj.getBorrowAmount()));
        //将保存的对象再返回去做数据的回显
        return R.ok().data("obj",one);
    }
    @ApiOperation("修改积分信息")
    @PutMapping
    public R update(
            @ApiParam(name = "integralGrade",value = "需要修改的对象",required = true)
            @RequestBody IntegralGrade integralGrade
    ){
        boolean b = IGService.updateById(integralGrade);
        Asserts.AssertTrue(b,ResponseEnum.UPDATE_FAIL);
        //根据修改的信息再查询一次,然后返回,做数据回显
        IntegralGrade in = IGService.getOne(Wrappers.lambdaQuery(IntegralGrade.class).eq(IntegralGrade::getId, integralGrade.getId()));

        return R.ok().data("obj",in);
    }
    @ApiOperation("根据id删除积分信息")
    @DeleteMapping("/{id}")
    public R removeById(
            @ApiParam(name = "id",value = "需要删除的积分信息的id",required = true)
            @PathVariable("id")Long id
    ){
        boolean b = IGService.removeById(id);
        Asserts.AssertTrue(b,ResponseEnum.DELETE_FAIL);
        return R.ok();
    }
    @ApiOperation("根据id批量删除积分信息")
    @DeleteMapping
    public R removeByIds(@ApiParam(name = "idList",value = "需要删除的积分信息的id集合",required = true)
                         @RequestBody List<Long> idList)
    {
        boolean b = IGService.removeByIds(idList);
        Asserts.AssertTrue(b,ResponseEnum.DELETE_FAIL);
        return R.ok();
    }
}

